package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smarttest.portal.mapper.DbAutoEnvironmentMapper;
import com.smarttest.portal.mapper.DbAutoInterfacescenedetailsMapper;
import com.smarttest.portal.mapper.DbAutoInterfacesummaryMapper;
import com.smarttest.portal.mapper.DbAutoModuleMapper;
import com.smarttest.portal.pojo.DbAutoEnvironment;
import com.smarttest.portal.pojo.DbAutoEnvironmentExample;
import com.smarttest.portal.pojo.DbAutoInterfacescenedetailsExample;
import com.smarttest.portal.pojo.DbAutoInterfacescenedetailsWithBLOBs;
import com.smarttest.portal.pojo.DbAutoInterfacesummary;
import com.smarttest.portal.pojo.DbAutoInterfacesummaryExample;
import com.smarttest.portal.pojo.DbAutoInterfacesummaryWithBLOBs;
import com.smarttest.portal.pojo.DbAutoModule;
import com.smarttest.portal.pojo.DbAutoModuleExample;
import com.smarttest.portal.service.Autocase_Mgmt_V2_Service;

@Service
public class Autocase_Mgmt_V2_Serviceimpl implements Autocase_Mgmt_V2_Service{
	@Autowired
    DbAutoInterfacesummaryMapper DbAutoInterfacesummarymapper;
	@Autowired
    DbAutoModuleMapper DbAutoModulemapper;
	@Autowired
	DbAutoEnvironmentMapper DbAutoEnvironmentMapper;
	@Autowired
	DbAutoInterfacescenedetailsMapper dbAutoInterfacescenedetailsMapper;
	@Override
	public List<DbAutoInterfacesummary> getInfByModule(List<Integer> ids) {
		List<DbAutoInterfacesummary> result = DbAutoInterfacesummarymapper.selectByModules(ids);
		return result;
	}
	
	@Override
	public List<DbAutoModule> getModuleByEnv(int envid) {
		// TODO Auto-generated method stub
		DbAutoModuleExample example = new DbAutoModuleExample();
		com.smarttest.portal.pojo.DbAutoModuleExample.Criteria criteria = example.createCriteria();
		criteria.andEidEqualTo(envid);
		List<DbAutoModule> result = DbAutoModulemapper.selectByExample(example);
		return result;
	}

	@Override
	public List<DbAutoInterfacesummaryWithBLOBs> getInfBySearch(int env,List<Integer> modules, List<Integer> ids, List<Integer> importants,
			List<String> owners, List<Integer> levellist) {
		// TODO Auto-generated method stub
		List<DbAutoInterfacesummaryWithBLOBs> result = DbAutoInterfacesummarymapper.selectBySearch(env, modules, ids, importants, owners);		
		Iterator<DbAutoInterfacesummaryWithBLOBs> it = result.iterator();
		while(it.hasNext()){
		    DbAutoInterfacesummaryWithBLOBs db = it.next();
		    List<Integer> l = dbAutoInterfacescenedetailsMapper.selectLevels(db.getId());
			if (l.isEmpty()) {
				it.remove();				
			}
			else if (null != levellist) {
				l.retainAll(levellist);
				if (l.isEmpty()) {
					it.remove();
				}
			}
		}
		return result;
	}

	@Override
	public int addAutoCase(String inf, int env, int url, String inftype, int degree, int module,
			String tester, String reqType, String outerparams,boolean dataAsUrlparam, String inits,List<DbAutoInterfacescenedetailsWithBLOBs> items) throws Exception {
		// TODO Auto-generated method stub
		int returnid = 0;
		DbAutoInterfacesummaryWithBLOBs infSummary = new DbAutoInterfacesummaryWithBLOBs();
		infSummary.setApiurlid(url);
		infSummary.setDegreeid(degree);
		infSummary.setEnvid(env);
		infSummary.setModuleid(module);
		infSummary.setName(inf);
		infSummary.setTester(tester);
		infSummary.setRequesttype(reqType);
		infSummary.setDisabled(false);
		infSummary.setOuterparams(outerparams);
		infSummary.setInnerparamToOuter(dataAsUrlparam);
		infSummary.setCreatetime(new Date());
		infSummary.setUpdatetime(new Date());
		infSummary.setDatainits(inits);
		DbAutoInterfacesummarymapper.insertAndGetId(infSummary);
		returnid = infSummary.getId();
		for(DbAutoInterfacescenedetailsWithBLOBs each:items){
			DbAutoInterfacescenedetailsWithBLOBs eachForInsert = new DbAutoInterfacescenedetailsWithBLOBs();
			eachForInsert.setDescription(each.getDescription());
			eachForInsert.setExpectcode(each.getExpectcode());
			eachForInsert.setExpectcount(each.getExpectcount());
			eachForInsert.setExpectmsg(each.getExpectmsg());
			eachForInsert.setInnerparams(each.getInnerparams());
			eachForInsert.setIntersumid(returnid);
			eachForInsert.setLevel(each.getLevel());
			eachForInsert.setResultasserts(each.getResultasserts());
			eachForInsert.setCreatetime(new Date());
			eachForInsert.setUpdatetime(new Date());
			int result = dbAutoInterfacescenedetailsMapper.insert(eachForInsert);
		}
		return returnid;
	}
	
	@Override
	public List<DbAutoEnvironment> initEnvSele(){
		DbAutoEnvironmentExample example = new DbAutoEnvironmentExample();
		List<DbAutoEnvironment> results = DbAutoEnvironmentMapper.selectByExample(example);
		return results;
	}

	@Override
	public List<DbAutoInterfacescenedetailsWithBLOBs> getInfDetialsBySummary(int summaryId, List<Integer> levellist) {
		// TODO Auto-generated method stub
		DbAutoInterfacescenedetailsExample example = new DbAutoInterfacescenedetailsExample();
		com.smarttest.portal.pojo.DbAutoInterfacescenedetailsExample.Criteria criteria = example.createCriteria();
		criteria.andIntersumidEqualTo(summaryId);
		if (null != levellist  && levellist.size() > 0) {
			criteria.andLevelIn(levellist);
		}
		List<DbAutoInterfacescenedetailsWithBLOBs> details =  dbAutoInterfacescenedetailsMapper.selectByExampleWithBLOBs(example);
		return details;
	}

	@Override
	public List<DbAutoInterfacesummary> getInfByEnv(int envid) {
		// TODO Auto-generated method stub
		DbAutoInterfacesummaryExample expample = new DbAutoInterfacesummaryExample();
		com.smarttest.portal.pojo.DbAutoInterfacesummaryExample.Criteria criteria = expample.createCriteria();
		criteria.andEnvidEqualTo(envid);
		List<DbAutoInterfacesummary> result = DbAutoInterfacesummarymapper.selectByExample(expample);
		return result;
	}

	@Override
	public List<DbAutoInterfacesummaryWithBLOBs> loadSummById(int id) {
		// TODO Auto-generated method stub
		DbAutoInterfacesummaryExample example = new DbAutoInterfacesummaryExample();
		com.smarttest.portal.pojo.DbAutoInterfacesummaryExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		List<DbAutoInterfacesummaryWithBLOBs> result = DbAutoInterfacesummarymapper.selectByExampleWithBLOBs(example);
		return result;
	}

	@Override
	public List<DbAutoInterfacescenedetailsWithBLOBs> loadDetailBySummaryId(int id) {
		// TODO Auto-generated method stub
		DbAutoInterfacescenedetailsExample example = new DbAutoInterfacescenedetailsExample();
		com.smarttest.portal.pojo.DbAutoInterfacescenedetailsExample.Criteria criteria = example.createCriteria();
		criteria.andIntersumidEqualTo(id);
		List<DbAutoInterfacescenedetailsWithBLOBs> result = dbAutoInterfacescenedetailsMapper.selectByExampleWithBLOBs(example);
		return result;
	}

	@Override
	public int updateAutoCase(int id, String inf, int env, int url, String inftype, int degree, int module,
			String tester, String reqType, String outerparams, boolean dataAsUrlparam,
			List<DbAutoInterfacescenedetailsWithBLOBs> items,List<Integer> deleteIds) throws Exception {
		// TODO Auto-generated method stub
		int updated = 0;
		for(DbAutoInterfacescenedetailsWithBLOBs each:items){
			int updateDetail;
			if(each.getId()==-1){
				each.setId(null);
				each.setIntersumid(id);
				each.setCreatetime(new Date());
				updateDetail = dbAutoInterfacescenedetailsMapper.insert(each);
			}else{
				each.setUpdatetime(new Date());
				updateDetail = dbAutoInterfacescenedetailsMapper.updateByPrimaryKeySelective(each);
			}
			updated += updateDetail;
		}
		for(int each:deleteIds){
			int deleteitems = dbAutoInterfacescenedetailsMapper.deleteByPrimaryKey(each);
			updated+=deleteitems;
		}
		DbAutoInterfacesummaryWithBLOBs summary = DbAutoInterfacesummarymapper.selectByPrimaryKey(id);
		if(summary!=null){
			summary.setApiurlid(url);
			summary.setDegreeid(degree);
			summary.setEnvid(env);
			summary.setModuleid(module);
			summary.setName(inf);
			summary.setTester(tester);
			summary.setRequesttype(reqType);
			summary.setDisabled(false);
			summary.setOuterparams(outerparams);
			summary.setInnerparamToOuter(dataAsUrlparam);
			summary.setUpdatetime(new Date());
			int resultSummary = DbAutoInterfacesummarymapper.updateByPrimaryKeySelective(summary);
			updated+=resultSummary;
		}
		return updated;
	}
}
